<html>
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>InheritableThreadLocal深度解析</title>
    <link href="https://cdn.staticfile.org/font-awesome/6.4.0/css/all.min.css" rel="stylesheet">
    <link href="https://cdn.staticfile.org/tailwindcss/2.2.19/tailwind.min.css" rel="stylesheet">
    <link href="https://fonts.googleapis.com/css2?family=Noto+Serif+SC:wght@400;500;600;700&family=Noto+Sans+SC:wght@300;400;500;700&display=swap" rel="stylesheet">
    <script src="https://cdn.jsdelivr.net/npm/mermaid@latest/dist/mermaid.min.js"></script>
    <style>
        body {
            font-family: 'Noto Sans SC', sans-serif;
            background-color: #f8fafc;
            color: #1e293b;
            line-height: 1.6;
        }
        h1, h2, h3, h4 {
            font-family: 'Noto Serif SC', serif;
            font-weight: 600;
            color: #1e293b;
        }
        .hero {
            background: linear-gradient(135deg, #3b82f6 0%, #6366f1 100%);
        }
        .code-block {
            background-color: #1e293b;
            border-radius: 0.5rem;
            padding: 1.5rem;
            position: relative;
        }
        .code-block pre {
            margin: 0;
            overflow-x: auto;
        }
        .code-block code {
            font-family: 'Courier New', Courier, monospace;
            color: #e2e8f0;
            font-size: 0.9rem;
        }
        .code-block .lang {
            position: absolute;
            top: 0.5rem;
            right: 1rem;
            color: #94a3b8;
            font-size: 0.8rem;
            text-transform: uppercase;
        }
        .card {
            background: white;
            border-radius: 0.75rem;
            box-shadow: 0 4px 6px -1px rgba(0, 0, 0, 0.1), 0 2px 4px -1px rgba(0, 0, 0, 0.06);
            transition: transform 0.2s, box-shadow 0.2s;
        }
        .card:hover {
            transform: translateY(-4px);
            box-shadow: 0 10px 15px -3px rgba(0, 0, 0, 0.1), 0 4px 6px -2px rgba(0, 0, 0, 0.05);
        }
        .icon-box {
            width: 3rem;
            height: 3rem;
            border-radius: 0.75rem;
            display: flex;
            align-items: center;
            justify-content: center;
            margin-bottom: 1rem;
        }
        .section-title {
            position: relative;
            padding-left: 1.5rem;
        }
        .section-title:before {
            content: '';
            position: absolute;
            left: 0;
            top: 0.5rem;
            height: 2rem;
            width: 0.5rem;
            border-radius: 0.25rem;
            background: linear-gradient(to bottom, #3b82f6, #6366f1);
        }
        .highlight {
            background: linear-gradient(120deg, rgba(59, 130, 246, 0.2) 0%, rgba(59, 130, 246, 0) 100%);
            padding: 0.25rem 0.5rem;
            border-radius: 0.25rem;
        }
        .note-box {
            border-left: 4px solid #3b82f6;
            background-color: #f1f5f9;
            padding: 1rem;
            border-radius: 0 0.5rem 0.5rem 0;
            margin: 1.5rem 0;
        }
        .note-box .note-title {
            font-weight: 600;
            margin-bottom: 0.5rem;
            color: #1e40af;
        }
    </style>
</head>
<body class="antialiased">
    <!-- Hero Section -->
    <section class="hero text-white py-20 px-4 md:px-0">
        <div class="container mx-auto max-w-5xl px-4">
            <div class="flex flex-col md:flex-row items-center">
                <div class="md:w-2/3 mb-10 md:mb-0">
                    <h1 class="text-4xl md:text-5xl font-bold leading-tight mb-6">深入解析InheritableThreadLocal</h1>
                    <p class="text-xl text-blue-100 mb-8">揭开父子线程间数据传递的神秘面纱，掌握多线程编程的关键技术</p>
                    <div class="flex flex-wrap gap-4">
                        <span class="bg-blue-500 bg-opacity-30 px-4 py-2 rounded-full text-sm flex items-center">
                            <i class="fas fa-microchip mr-2"></i> Java多线程
                        </span>
                        <span class="bg-blue-500 bg-opacity-30 px-4 py-2 rounded-full text-sm flex items-center">
                            <i class="fas fa-code-branch mr-2"></i> 线程数据传递
                        </span>
                        <span class="bg-blue-500 bg-opacity-30 px-4 py-2 rounded-full text-sm flex items-center">
                            <i class="fas fa-shield-alt mr-2"></i> 线程安全
                        </span>
                    </div>
                </div>
                <div class="md:w-1/3 flex justify-center">
                    <div class="bg-white bg-opacity-20 p-6 rounded-2xl backdrop-blur-sm border border-white border-opacity-20">
                        <div class="mermaid max-w-xs">
                            graph TD
                                A[父线程] -->|创建| B[子线程]
                                A -->|InheritableThreadLocal| B
                                B --> C[获取父线程数据]
                        </div>
                    </div>
                </div>
            </div>
        </div>
    </section>

    <!-- Main Content -->
    <main class="container mx-auto max-w-5xl px-4 py-12">
        <!-- Introduction -->
        <section class="mb-16">
            <div class="bg-white rounded-xl p-8 card">
                <p class="text-lg text-slate-700 mb-6">在Java多线程开发中，你是否遇到过这样的场景：父线程创建了子线程，却发现子线程无法访问父线程中的某些数据？或者明明设置了ThreadLocal变量，子线程却读取不到？这些问题背后，往往隐藏着<span class="highlight">InheritableThreadLocal</span>这个容易被忽视的类。</p>
                
                <div class="flex justify-center my-8">
                    <img src="https://cdn.nlark.com/yuque/0/2025/png/21449790/1755836111927-11cf4a25-4e1f-4fa2-9f0e-b0c8f0fbe67a.png" alt="ThreadLocal与InheritableThreadLocal对比" class="rounded-lg shadow-md max-w-full">
                </div>
                
                <p class="text-lg text-slate-700">ThreadLocal解决了线程内部数据隔离的问题，但InheritableThreadLocal却能在父子线程之间架起一座数据传递的桥梁。然而，这座桥梁并非总是如你想象的那样可靠。从阿里巴巴的分布式链路追踪，到Spring的事务传播，再到微服务架构中的用户上下文传递，InheritableThreadLocal的身影无处不在。</p>
            </div>
        </section>

        <!-- Section 1 -->
        <section class="mb-16">
            <h2 class="text-3xl font-bold mb-8 section-title">1：InheritableThreadLocal的前世今生</h2>
            
            <div class="grid md:grid-cols-2 gap-8 mb-12">
                <div class="card p-6">
                    <h3 class="text-2xl font-semibold mb-4 flex items-center">
                        <span class="icon-box bg-blue-100 text-blue-600 mr-4">
                            <i class="fas fa-history"></i>
                        </span>
                        1.1：从ThreadLocal到InheritableThreadLocal的演进
                    </h3>
                    <p class="text-slate-700">ThreadLocal的出现解决了多线程环境下数据隔离的问题，每个线程都有自己独立的数据副本。但在实际开发中，我们经常需要子线程能够访问父线程的数据，比如用户ID、事务ID等上下文信息。</p>
                    
                    <div class="code-block mt-4">
                        <span class="lang">java</span>
                        <pre><code>// 传统ThreadLocal的问题
public class UserContext {
    private static final ThreadLocal&lt;String&gt; userThreadLocal = new ThreadLocal&lt;&gt;();
    
    public static void setUser(String userId) {
        userThreadLocal.set(userId);
    }
    
    public static String getUser() {
        return userThreadLocal.get();
    }
}

// 主线程设置用户ID
UserContext.setUser("user123");

// 创建子线程
Thread childThread = new Thread(() -> {
    // 子线程无法获取父线程的用户ID
    System.out.println("子线程用户ID: " + UserContext.getUser()); // 输出: null
});
childThread.start();</code></pre>
                    </div>
                </div>
                
                <div class="card p-6">
                    <h3 class="text-2xl font-semibold mb-4 flex items-center">
                        <span class="icon-box bg-indigo-100 text-indigo-600 mr-4">
                            <i class="fas fa-project-diagram"></i>
                        </span>
                        1.2：设计初衷与使用场景
                    </h3>
                    <p class="text-slate-700">InheritableThreadLocal的设计初衷是为了解决父子线程间的数据传递问题。它主要应用于以下场景：</p>
                    
                    <ul class="list-disc pl-5 mt-4 space-y-2 text-slate-700">
                        <li><span class="font-medium">分布式链路追踪</span>：传递traceId、spanId等追踪信息</li>
                        <li><span class="font-medium">用户上下文传递</span>：传递用户ID、权限信息等</li>
                        <li><span class="font-medium">事务上下文传播</span>：传递事务ID、连接信息等</li>
                        <li><span class="font-medium">日志上下文</span>：传递请求ID、操作人等标识信息</li>
                    </ul>
                    
                    <div class="note-box mt-6">
                        <div class="note-title"><i class="fas fa-lightbulb mr-2"></i>最佳实践</div>
                        <p>在需要跨线程传递上下文信息的场景中，优先考虑使用InheritableThreadLocal而不是共享变量，因为它提供了更好的线程隔离性和安全性。</p>
                    </div>
                </div>
            </div>
            
            <div class="card p-6">
                <h3 class="text-2xl font-semibold mb-4 flex items-center">
                    <span class="icon-box bg-purple-100 text-purple-600 mr-4">
                        <i class="fas fa-sitemap"></i>
                    </span>
                    1.3：在JDK中的位置与继承关系
                </h3>
                <p class="text-slate-700">InheritableThreadLocal位于<code>java.lang</code>包中，继承自ThreadLocal类：</p>
                
                <div class="code-block mt-4">
                    <span class="lang">java</span>
                    <pre><code>public class InheritableThreadLocal&lt;T&gt; extends ThreadLocal&lt;T&gt; {
    protected T childValue(T parentValue) {
        return parentValue;
    }
}</code></pre>
                </div>
                
                <div class="mermaid mt-6">
                    classDiagram
                        ThreadLocal &lt;|-- InheritableThreadLocal
                        class ThreadLocal {
                            +get()
                            +set()
                            +remove()
                        }
                        class InheritableThreadLocal {
                            +childValue()
                        }
                </div>
            </div>
        </section>

        <!-- Section 2 -->
        <section class="mb-16">
            <h2 class="text-3xl font-bold mb-8 section-title">2：核心原理深度剖析</h2>
            
            <div class="grid md:grid-cols-3 gap-6 mb-8">
                <div class="card p-6">
                    <h3 class="text-xl font-semibold mb-3">2.1：ThreadLocalMap的内部结构</h3>
                    <p class="text-slate-700 text-sm">ThreadLocalMap是ThreadLocal的核心数据结构，每个Thread对象都维护一个ThreadLocalMap实例。</p>
                    
                    <div class="code-block mt-4">
                        <span class="lang">java</span>
                        <pre><code>// Thread类中的关键字段
public class Thread implements Runnable {
    ThreadLocal.ThreadLocalMap threadLocals = null;
    ThreadLocal.ThreadLocalMap inheritableThreadLocals = null;
}</code></pre>
                    </div>
                </div>
                
                <div class="card p-6">
                    <h3 class="text-xl font-semibold mb-3">2.2：父子线程创建时的数据传递机制</h3>
                    <p class="text-slate-700 text-sm">当创建子线程时，JVM会调用<code>init</code>方法，其中包含数据传递的逻辑：</p>
                    
                    <div class="code-block mt-4">
                        <span class="lang">java</span>
                        <pre><code>private void init(ThreadGroup g, Runnable target, String name,
                  long stackSize, AccessControlContext acc,
                  boolean inheritThreadLocals) {
    if (inheritThreadLocals && parent.inheritableThreadLocals != null) {
        this.inheritableThreadLocals = 
            ThreadLocal.createInheritedMap(parent.inheritableThreadLocals);
    }
}</code></pre>
                    </div>
                </div>
                
                <div class="card p-6">
                    <h3 class="text-xl font-semibold mb-3">2.3：浅拷贝与深拷贝的区别</h3>
                    <p class="text-slate-700 text-sm">InheritableThreadLocal默认进行浅拷贝，这意味着：</p>
                    
                    <div class="code-block mt-4">
                        <span class="lang">java</span>
                        <pre><code>// 子线程修改对象会影响父线程
childUser.setName("李四");
// 父线程的用户信息也被修改了
System.out.println("父线程用户: " + userInheritableThreadLocal.get().getName());</code></pre>
                    </div>
                </div>
            </div>
            
            <div class="card p-6">
                <h3 class="text-2xl font-semibold mb-4">完整示例：浅拷贝演示</h3>
                <div class="code-block">
                    <span class="lang">java</span>
                    <pre><code>public class InheritableThreadLocalDemo {
    private static final InheritableThreadLocal&lt;User&gt; userInheritableThreadLocal = 
        new InheritableThreadLocal&lt;&gt;();
    
    public static void main(String[] args) {
        User user = new User("张三", 25);
        userInheritableThreadLocal.set(user);
        
        Thread childThread = new Thread(() -> {
            User childUser = userInheritableThreadLocal.get();
            System.out.println("子线程用户: " + childUser.getName());
            
            // 修改用户信息会影响父线程
            childUser.setName("李四");
        });
        
        childThread.start();
        
        try {
            childThread.join();
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        
        // 父线程的用户信息也被修改了
        System.out.println("父线程用户: " + userInheritableThreadLocal.get().getName());
    }
}</code></pre>
                </div>
            </div>
        </section>

        <!-- Section 3 -->
        <section class="mb-16">
            <h2 class="text-3xl font-bold mb-8 section-title">3：实战应用场景</h2>
            
            <div class="grid md:grid-cols-3 gap-6 mb-8">
                <div class="card p-6">
                    <h3 class="text-xl font-semibold mb-3">3.1：分布式链路追踪中的用户ID传递</h3>
                    <p class="text-slate-700 text-sm">在微服务架构中，需要传递traceId来追踪整个调用链路：</p>
                    
                    <div class="code-block mt-4">
                        <span class="lang">java</span>
                        <pre><code>// 在HTTP拦截器中设置
@Component
public class TraceInterceptor implements HandlerInterceptor {
    @Override
    public boolean preHandle(HttpServletRequest request, 
                           HttpServletResponse response, Object handler) {
        String traceId = request.getHeader("X-Trace-Id");
        TraceContext.setTraceId(traceId);
        return true;
    }
}</code></pre>
                    </div>
                </div>
                
                <div class="card p-6">
                    <h3 class="text-xl font-semibold mb-3">3.2：Spring事务上下文传播</h3>
                    <p class="text-slate-700 text-sm">事务上下文通过TransactionSynchronizationManager管理：</p>
                    
                    <div class="code-block mt-4">
                        <span class="lang">java</span>
                        <pre><code>@Service
public class UserService {
    @Transactional
    public void createUser(User user) {
        // 异步发送邮件通知
        CompletableFuture.runAsync(() -> {
            // 子线程仍然可以访问事务上下文
            String transactionName = 
                TransactionSynchronizationManager.getCurrentTransactionName();
        });
    }
}</code></pre>
                    </div>
                </div>
                
                <div class="card p-6">
                    <h3 class="text-xl font-semibold mb-3">3.3：微服务架构中的请求上下文</h3>
                    <p class="text-slate-700 text-sm">在微服务调用链中传递请求上下文信息：</p>
                    
                    <div class="code-block mt-4">
                        <span class="lang">java</span>
                        <pre><code>public class RequestContext {
    private static final InheritableThreadLocal&lt;Map&lt;String, Object&gt;&gt; contextThreadLocal = 
        new InheritableThreadLocal&lt;&gt;();
    
    public static void setContext(String key, Object value) {
        Map&lt;String, Object&gt; context = contextThreadLocal.get();
        if (context == null) {
            context = new HashMap&lt;&gt;();
            contextThreadLocal.set(context);
        }
        context.put(key, value);
    }
}</code></pre>
                    </div>
                </div>
            </div>
        </section>

        <!-- Section 4 -->
        <section class="mb-16">
            <h2 class="text-3xl font-bold mb-8 section-title">4：常见陷阱与解决方案</h2>
            
            <div class="grid md:grid-cols-2 gap-8">
                <div class="card p-6">
                    <h3 class="text-2xl font-semibold mb-4 flex items-center">
                        <span class="icon-box bg-red-100 text-red-600 mr-4">
                            <i class="fas fa-exclamation-triangle"></i>
                        </span>
                        4.1：线程池环境下的数据丢失问题
                    </h3>
                    <p class="text-slate-700">线程池中的线程是复用的，不会重新创建，因此数据传递失效：</p>
                    
                    <div class="code-block mt-4">
                        <span class="lang">java</span>
                        <pre><code>private static final ExecutorService executorService = 
    Executors.newFixedThreadPool(2);

public static void main(String[] args) {
    inheritableThreadLocal.set("用户" + index);
    executorService.submit(() -> {
        // 可能获取不到正确的值
        System.out.println("获取到用户: " + inheritableThreadLocal.get());
    });
}</code></pre>
                    </div>
                    
                    <div class="note-box mt-6 bg-amber-50 border-l-amber-400">
                        <div class="note-title text-amber-800"><i class="fas fa-lightbulb mr-2"></i>解决方案</div>
                        <p>使用TransmittableThreadLocal或者手动传递数据：</p>
                        <div class="code-block mt-2">
                            <span class="lang">java</span>
                            <pre><code>// 方案1：手动传递
executorService.submit(() -> {
    inheritableThreadLocal.set(userId);
    try {
        doBusinessLogic();
    } finally {
        inheritableThreadLocal.remove();
    }
});

// 方案2：使用装饰器模式
public class ContextAwareRunnable implements Runnable {
    private final String userId;
    
    @Override
    public void run() {
        inheritableThreadLocal.set(userId);
        try {
            delegate.run();
        } finally {
            inheritableThreadLocal.remove();
        }
    }
}</code></pre>
                        </div>
                    </div>
                </div>
                
                <div class="card p-6">
                    <h3 class="text-2xl font-semibold mb-4 flex items-center">
                        <span class="icon-box bg-yellow-100 text-yellow-600 mr-4">
                            <i class="fas fa-memory"></i>
                        </span>
                        4.3：内存泄漏的预防措施
                    </h3>
                    <p class="text-slate-700">InheritableThreadLocal如果不及时清理，会造成内存泄漏：</p>
                    
                    <div class="code-block mt-4">
                        <span class="lang">java</span>
                        <pre><code>public class MemoryLeakPreventionDemo {
    public static void main(String[] args) {
        try {
            doBusinessLogic();
        } finally {
            // 重要：清理ThreadLocal
            inheritableThreadLocal.remove();
        }
    }
}</code></pre>
                    </div>
                    
                    <div class="mt-6 p-4 bg-slate-50 rounded-lg">
                        <h4 class="font-semibold text-slate-800 mb-2">内存泄漏原理：</h4>
                        <p class="text-slate-700 text-sm">ThreadLocalMap中的Entry是弱引用Key，但如果Value是强引用，且线程不终止，会导致Value无法回收。</p>
                        
                        <div class="mermaid mt-4">
                            graph LR
                                A[Thread] --> B[ThreadLocalMap]
                                B --> C[Entry]
                                C -->|弱引用| D[ThreadLocal]
                                C -->|强引用| E[Value]
                        </div>
                    </div>
                </div>
            </div>
        </section>

        <!-- Section 5 -->
        <section class="mb-16">
            <h2 class="text-3xl font-bold mb-8 section-title">5：性能优化与最佳实践</h2>
            
            <div class="grid md:grid-cols-2 gap-8">
                <div class="card p-6">
                    <h3 class="text-2xl font-semibold mb-4">5.1：合理使用InheritableThreadLocal的时机</h3>
                    
                    <div class="grid grid-cols-2 gap-4">
                        <div class="bg-green-50 p-4 rounded-lg">
                            <h4 class="font-semibold text-green-800 mb-2 flex items-center">
                                <i class="fas fa-check-circle mr-2"></i>适用场景
                            </h4>
                            <ul class="list-disc pl-5 text-sm text-green-700">
                                <li>父子线程关系明确</li>
                                <li>数据量较小</li>
                                <li>数据生命周期与线程生命周期一致</li>
                            </ul>
                        </div>
                        
                        <div class="bg-red-50 p-4 rounded-lg">
                            <h4 class="font-semibold text-red-800 mb-2 flex items-center">
                                <i class="fas fa-times-circle mr-2"></i>不适用场景
                            </h4>
                            <ul class="list-disc pl-5 text-sm text-red-700">
                                <li>线程池环境</li>
                                <li>异步任务</li>
                                <li>数据量大的场景</li>
                            </ul>
                        </div>
                    </div>
                    
                    <div class="mt-6">
                        <h4 class="font-semibold text-slate-800 mb-2">性能考虑：</h4>
                        <p class="text-slate-700">InheritableThreadLocal在创建线程时会进行一次数据拷贝，对性能有轻微影响。在需要频繁创建线程的场景中应考虑性能开销。</p>
                    </div>
                </div>
                
                <div class="card p-6">
                    <h3 class="text-2xl font-semibold mb-4">5.2：与其他线程安全方案的对比</h3>
                    
                    <div class="overflow-x-auto">
                        <table class="min-w-full bg-white">
                            <thead>
                                <tr class="bg-gray-100">
                                    <th class="py-2 px-4 text-left">方案</th>
                                    <th class="py-2 px-4 text-left">适用场景</th>
                                    <th class="py-2 px-4 text-left">性能</th>
                                </tr>
                            </thead>
                            <tbody>
                                <tr class="border-b">
                                    <td class="py-2 px-4">InheritableThreadLocal</td>
                                    <td class="py-2 px-4">父子线程数据传递</td>
                                    <td class="py-2 px-4">中等</td>
                                </tr>
                                <tr class="border-b">
                                    <td class="py-2 px-4">ThreadLocal</td>
                                    <td class="py-2 px-4">线程内数据隔离</td>
                                    <td class="py-2 px-4">高</td>
                                </tr>
                                <tr class="border-b">
                                    <td class="py-2 px-4">原子引用</td>
                                    <td class="py-2 px-4">线程间共享数据</td>
                                    <td class="py-2 px-4">高</td>
                                </tr>
                                <tr>
                                    <td class="py-2 px-4">同步块</td>
                                    <td class="py-2 px-4">线程安全访问</td>
                                    <td class="py-2 px-4">低</td>
                                </tr>
                            </tbody>
                        </table>
                    </div>
                </div>
            </div>
        </section>

        <!-- Section 6 -->
        <section>
            <h2 class="text-3xl font-bold mb-8 section-title">6：源码分析与实现细节</h2>
            
            <div class="grid md:grid-cols-2 gap-8 mb-8">
                <div class="card p-6">
                    <h3 class="text-2xl font-semibold mb-4">6.1：createMap方法的实现逻辑</h3>
                    <p class="text-slate-700">ThreadLocal.createInheritedMap方法是数据传递的核心：</p>
                    
                    <div class="code-block mt-4">
                        <span class="lang">java</span>
                        <pre><code>static ThreadLocalMap createInheritedMap(ThreadLocalMap parentMap) {
    ThreadLocalMap newMap = new ThreadLocalMap(parentMap);
    return newMap;
}

private ThreadLocalMap(ThreadLocalMap parentMap) {
    Entry[] parentTable = parentMap.table;
    int len = parentTable.length;
    
    for (int j = 0; j < len; j++) {
        Entry e = parentTable[j];
        if (e != null) {
            ThreadLocal&lt;Object&gt; key = (ThreadLocal&lt;Object&gt;) e.get();
            if (key != null) {
                // 关键：调用childValue方法
                Object value = key.childValue(e.value);
                Entry c = new Entry(key, value);
                int h = key.threadLocalHashCode & (len - 1);
                while (table[h] != null)
                    h = nextIndex(h, len);
                table[h] = c;
                size++;
            }
        }
    }
}</code></pre>
                    </div>
                </div>
                
                <div class="card p-6">
                    <h3 class="text-2xl font-semibold mb-4">6.2：childValue方法的扩展点</h3>
                    <p class="text-slate-700">InheritableThreadLocal通过重写childValue方法提供了扩展能力：</p>
                    
                    <div class="code-block mt-4">
                        <span class="lang">java</span>
                        <pre><code>public class CustomInheritableThreadLocal&lt;T&gt; extends InheritableThreadLocal&lt;T&gt; {
    @Override
    protected T childValue(T parentValue) {
        if (parentValue instanceof String) {
            // 对字符串类型进行特殊处理
            return (T) ("子线程_" + parentValue);
        }
        return parentValue;
    }
}</code></pre>
                    </div>
                    
                    <div class="note-box mt-6">
                        <div class="note-title"><i class="fas fa-info-circle mr-2"></i>扩展应用</div>
                        <p>可以通过重写childValue方法实现深拷贝、数据转换等高级功能，满足特定业务场景的需求。</p>
                    </div>
                </div>
            </div>
            
            <div class="card p-6">
                <h3 class="text-2xl font-semibold mb-4">6.3：与ThreadLocal的差异分析</h3>
                
                <div class="grid md:grid-cols-2 gap-6">
                    <div>
                        <h4 class="font-semibold text-lg mb-3 text-blue-600">ThreadLocal</h4>
                        <ul class="list-disc pl-5 text-slate-700 space-y-2">
                            <li>线程内部数据隔离</li>
                            <li>子线程无法访问父线程数据</li>
                            <li>适用于完全隔离的场景</li>
                            <li>性能略高</li>
                        </ul>
                    </div>
                    
                    <div>
                        <h4 class="font-semibold text-lg mb-3 text-indigo-600">InheritableThreadLocal</h4>
                        <ul class="list-disc pl-5 text-slate-700 space-y-2">
                            <li>父子线程间数据传递</li>
                            <li>子线程可访问父线程数据</li>
                            <li>适用于上下文传递场景</li>
                            <li>创建线程时有一次拷贝开销</li>
                        </ul>
                    </div>
                </div>
                
                <div class="code-block mt-6">
                    <span class="lang">java</span>
                    <pre><code>public class ThreadLocalVsInheritableThreadLocal {
    public static void main(String[] args) {
        ThreadLocal&lt;String&gt; threadLocal = new ThreadLocal&lt;&gt;();
        threadLocal.set("ThreadLocal数据");
        
        InheritableThreadLocal&lt;String&gt; inheritableThreadLocal = 
            new InheritableThreadLocal&lt;&gt;();
        inheritableThreadLocal.set("InheritableThreadLocal数据");
        
        Thread childThread = new Thread(() -> {
            System.out.println("ThreadLocal: " + threadLocal.get()); // null
            System.out.println("InheritableThreadLocal: " + 
                inheritableThreadLocal.get()); // 输出数据
        });
        
        childThread.start();
    }
}</code></pre>
                </div>
            </div>
        </section>
    </main>

    <script>
        mermaid.initialize({
            startOnLoad: true,
            theme: 'default',
            flowchart: {
                useMaxWidth: true,
                htmlLabels: true,
                curve: 'basis'
            }
        });
    </script>
</body>
</html>